//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <winhack.h>
#include <elasys_server.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <disk.h>

int DriveRW(unsigned long Position, void *pBuffer, int Size, int Flags);

ECode CDisk::Read(
    /* [in] */ Int64 u64Offset,
    /* [in] */ Int32 bytesToRead,
    /* [out] */ MemoryBuf * pBuffer,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    int ret;
    assert((bytesToRead & 0x1ff) == 0);
    if (pBuffer == NULL) {
        return E_INVALID_ARGUMENT;
    }
    ret = DriveRW((unsigned long)u64Offset, pBuffer->GetPayload(), bytesToRead, 0);
    if (ret == (int)bytesToRead) {
        pBuffer->SetUsed(ret);
        return NOERROR;
    }
    wprintf(L"CDisk::Read: At file %d, line %d.\n", __FILE__, __LINE__);
    return E_INVALID_OPERATION;
}

ECode CDisk::Write(
    /* [in] */ Int64 u64Offset,
    /* [in] */ const MemoryBuf & buffer,
    /* [out] */ Int32 * pBytesWritten,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    int ret;
    assert((buffer.GetUsed() & 0x1ff) == 0);
    ret = DriveRW((unsigned long)u64Offset, buffer.GetPayload(), buffer.GetUsed(), 1);
    if (ret == buffer.GetUsed()) {
        if (pBytesWritten) *pBytesWritten = ret;
        return NOERROR;
    }
    wprintf(L"CDisk::Write: At file %d, line %d.\n", __FILE__, __LINE__);
    return E_INVALID_OPERATION;
}

ECode CDisk::Control(
    /* [in] */ Handle32 nControlCode,
    /* [in] */ const MemoryBuf & inBuffer,
    /* [out] */ MemoryBuf * pOutBuffer,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    return E_NOT_IMPLEMENTED;
}
